//
//  JFMeViewController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 16/1/14.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//
///******
// ******* 个人中心
// ******* */

#import "JFMeViewController.h"
#import "SHBankCardManagerController.h"
#import "SHUserResetPasswordController.h"
#import "SHUserPayPasswordController.h"
#import "SHModifyUserInfoViewController.h"
#import "PartnerViewController.h"
#import "SetViewController.h"
#import "JCMineHead.h"
@interface JFMeViewController ()<MineViewDelegate>

@property (nonatomic, strong) JCMineHead * headView;

@property (nonatomic, strong) UIButton * passwordBtn;

@property (nonatomic, strong) UIButton * setBtn;

@property (nonatomic, strong) UIButton * outBtn;//退出

@property (nonatomic, strong) UserModel *userModel;


@end

@implementation JFMeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self getUserInfo];

}

#pragma mark - 设置状态栏字体颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return  UIStatusBarStyleLightContent;
}

#pragma mark - 视图已经加载
- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self initUI];
    
}

- (void)initUI{
   
    self.headView = [[JCMineHead alloc] initWithFrame:CGRectZero];
    self.headView.delegate = self;
    [self.view addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
    }];
    
    UIView * setView = [UIView new];
    setView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:setView];
    
    [setView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom).offset(10);
        make.left.right.equalTo(self.view);
    }];
    
    self.passwordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.passwordBtn setImage:[UIImage imageNamed:@"my_password"] forState:UIControlStateNormal];
    self.passwordBtn.tag = 1002;
    [self.passwordBtn addTarget:self action:@selector(didMineViewBtnAction:) forControlEvents:UIControlEventTouchUpInside
     ];
    [setView addSubview:self.passwordBtn];
    
    
    [self.passwordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(setView).offset(__I6Width(80));
        make.top.equalTo(setView).offset(__I6Height(25));
    }];
    
   
    self.setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.setBtn setImage:[UIImage imageNamed:@"my_set"] forState:UIControlStateNormal];
    self.setBtn.tag = 1004;
    [self.setBtn addTarget:self action:@selector(didMineViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [setView addSubview:self.setBtn];
    
    [self.setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(setView).offset(-__I6Width(80));
        make.top.equalTo(setView).offset(__I6Height(25));
    }];
    
    UILabel * setLabel = [UILabel new];
    setLabel.text = @"设置";
    setLabel.textColor = [UIColor colorWithHexString:@"#2C2C2C"];
    setLabel.font = KSHSystemFont(15);
    [setView addSubview:setLabel];
    [setLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.setBtn.mas_bottom);
        make.centerX.equalTo(self.setBtn);
        make.bottom.equalTo(setView).offset(-__I6Height(24));
    }];
    
    
    UIView * serviceview = [UIView new];
    serviceview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:serviceview];
    
    [serviceview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(setView.mas_bottom).offset(10);
        make.height.mas_offset(__I6Height(40));
    }];
    
    UIImageView * serImage = [UIImageView new];
    serImage.image = [UIImage imageNamed:@"service"];
    [serviceview addSubview:serImage];
    [serImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(serviceview);
        make.left.equalTo(serviceview).offset(7);
    }];
    
    UILabel * serLabel = [UILabel new];
    serLabel.text = @"联系客服";
    serLabel.textColor = [UIColor colorWithHexString:@"333333"];
    serLabel.font = KSHSystemFont(15);
    [serviceview addSubview:serLabel];
    
    [serLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(serviceview);
        make.left.equalTo(serImage.mas_right).offset(6);
    }];
    
    UILabel * phoneLabel = [UILabel new];
    phoneLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    phoneLabel.text = @"400-8843-525";
    phoneLabel.font = KSHSystemFont(12);
    [serviceview addSubview:phoneLabel];
    
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(serviceview);
        make.left.equalTo(serLabel.mas_right).offset(11);
    }];
    
    
    self.outBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.outBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.outBtn setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
    self.outBtn.titleLabel.font = KSHSystemFont(16);
    self.outBtn.layer.masksToBounds = YES;
    self.outBtn.layer.cornerRadius = __I6Height(40)/2;
    self.outBtn.layer.borderWidth = 1;
    self.outBtn.layer.borderColor = [UIColor colorWithHexString:@"#999999"].CGColor;
    self.outBtn.tag = 1005;
    [self.outBtn addTarget:self action:@selector(didMineViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.outBtn];
    
    [self.outBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(serviceview.mas_bottom).offset(__I6Height(22));
        make.centerX.equalTo(self.view);
        make.height.mas_offset(__I6Height(40));
        make.width.mas_equalTo(__I6Width(270));
    }];
}

- (void)didMineViewBtnAction:(UIButton *)btn{
    if (btn.tag == 1000) {
        if (btn.selected == YES) {
            [self.view makeToast:@"实名认证已通过" duration:2 position:@"center"];
        }else{
            [self gotoCertificationVc];
        }
    }else if (btn.tag == 1001){
        if (btn.selected == YES) {
            [self.navigationController pushViewController:[[SHBankCardManagerController alloc]init] animated:YES];
        }else{
            [self gotoCertificationVc];
        }
    }else if (btn.tag == 1002){
        if ([UserModel shareManager].has_pay_password ) {
            SHUserResetPasswordController *payPassWordVc = [[SHUserResetPasswordController alloc]init];
            [self.navigationController pushViewController:payPassWordVc animated:YES];
        }else{//设置支付密码
            SHUserPayPasswordController *payPassWordVc = [[SHUserPayPasswordController alloc]init];
            [self.navigationController pushViewController:payPassWordVc animated:YES];
        }
    }else if (btn.tag == 1004){
        SetViewController * setVC = [[SetViewController alloc] init];
        [self.navigationController pushViewController:setVC animated:YES];
    }else{
        [self signOut];
    }
}

- (void)gotoCertificationVc{
    [self.navigationController pushViewController:[[SHRealNameViewController alloc]init] animated:YES];
}


- (void)didUserNameAction{
    SHModifyUserInfoViewController *nickNameVC = [[SHModifyUserInfoViewController alloc] init];
    
    [self.navigationController pushViewController:nickNameVC animated:YES];
}

- (void)signOut{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:Kalogout httpMethod:@"POST" params:nil data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES ];
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            [Tool setObject:nil forKey:@"isSdUser"];
            [Tool setObject:nil forKey:KLoginStatusCookie];
            //清除 实名，银行卡信息完善状态
            [Tool setBool:NO forKey:KUserAuthBankStatus];
            JFLoginViewController * loginVC = [[JFLoginViewController alloc] init];
            UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
            
        }
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES ];
    }];
}

#pragma mark - 网络请求
- (void)getUserInfo{
    [MyDataService requestAFURL:Klogin  httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        
        if ([result[@"success"]isEqualToNumber:@(1)] && [result[@"data"] count] != 0) {
            self.userModel = [UserModel mj_objectWithKeyValues:result[@"data"]];
            [[UserModel shareManager] setMyUserModel:self.userModel];
            self.headView.userModel = self.userModel;
            [self.headView reloadInputViews];
        }else{
            if ([result[@"data"] isKindOfClass:[NSString class]]) {
                [[UIApplication sharedApplication].keyWindow makeToast:result[@"data"] duration:2.f position:@"center"];
            }
        }
    } failure:^(id erro) {
    }];
}


@end

